<?php

if (!function_exists('abort')) {
    function abort($code, $message = '')
    {
        if (!$message) {
            $message = Response::$statusTexts[$code] ?? 'unknown error';
        }
        throw new Exception($message, $code);
    }
}

if (!function_exists('env')) {
    function env($name, $default = null)
    {
        $result = getenv($name);
        return $result ?: $default;
    }
}

if (!function_exists('camelCase')) {
    //下划线转驼峰
    function camelCase($string, $separator = '_')
    {
        $string = $separator . str_replace($separator, ' ', strtolower($string));
        return str_replace(' ', '', ucwords(ltrim($string, $separator)));
    }
}

if (!function_exists('unCamelCase')) {
    //驼峰转下划线
    function unCamelCase(string $camelCaps, string $separator = '_')
    {
        return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
    }
}

function getUser($request=null){
    if(!$request){
        $request=request();
    }
    $token=$request->header('Authorization');
    if(config('app.debug')){
        if(is_numeric($token)){
            return \app\model\UserModel::find($token);
        }
    }
    return \Tinywan\Jwt\JwtToken::getUser();
}







